<?php
namespace app\models;
use ac\component\tool\Tool;
use app\models\Basic;

use Yii;

class Experts extends Basic
{
    
    public static function tableName()
    {
        return '{{%pj8_experts}}';
    }

    public function rules()
    {
        return [
            ['member_id', 'integer', 'message' => 'member_id必须为数字，或数字字符串', 
                         'min'=>1, 'tooSmall' => 'member_id最小存储1'],
            ['name', 'required', 'message' => 'name不能为空', 'on' => 'add'],
            ['name', 'string', 'message' => 'name必须是字符串',
                                'min' => 1, 'tooShort' => 'name最短1位',
                                'max' => 16, 'tooLong' => 'name最长16位'],
            ['avatar', 'string', 'message' => 'avatar必须是字符串',
                                'max' => 512, 'tooLong' => 'avatar最长512位'],
            ['goodat', 'string', 'message' => 'goodat必须是字符串',
                                'max' => 64, 'tooLong' => 'goodat最长64位'],
            ['authenticate_cost', 'integer', 'message' => 'authenticate_cost必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'authenticate_cost最小存储0', 
                        'max'=>9999, 'tooBig' => 'authenticate_cost最大存储9999'],
            ['identified', 'integer', 'message' => 'identified必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'identified最小存储0', 
                        'max'=>9999, 'tooBig' => 'identified最大存储9999'],
            ['introduce', 'string', 'message' => 'introduce必须是字符串',
                                'max' => 512, 'tooLong' => 'introduce最长512位'],
            ['sort', 'integer', 'message' => 'sort必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'sort最小存储0', 
                     'max'=>9999, 'tooBig' => 'sort最大存储9999'],
            ['display', 'integer', 'message' => 'display必须为数字，或数字字符串'],
            ['display', 'in', 'range' => array(1, 2), 'message' => 'display值必须在1,2中选择'],      
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加*/
    public function addExperts($data)
    {
        // P($data);
        $this->scenario = 'add';
        if(isset($data['Experts']['avatar']) and is_array($data['Experts']['avatar'])){
            $data['Experts']['avatar'] = Tool::changeUploadFormat($data['Experts']['avatar']);
        }
        if(isset($data['Experts']['display'])){
            $data['Experts']['display'] = Tool::changeBool($data['Experts']['display']);
        }
        if(!isset($data['Experts']['add_time']) or empty($data['Experts']['add_time'])){
            $data['Experts']['add_time'] = date('Y-m-d H:i:s', time());
        }
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                $id = $this->getPrimaryKey();
                $this->updateAll(['sort' => $id], 'id = :id', [':id' => $id]);
                return $id;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    $app_id 
    */
    public function modifyExperts($data, $id)
    {
        // P($data);
        $experts = self::find()->where('id = :id', [':id' => $id])->one();
        // P($experts);
        if(isset($data['Experts']['avatar']) and is_array($data['Experts']['avatar'])){
            $data['Experts']['avatar'] = Tool::changeUploadFormat($data['Experts']['avatar']);
        }
        if(isset($data['Experts']['display'])){
            $data['Experts']['display'] = Tool::changeBool($data['Experts']['display']);
        }
        // P($data);
        if(!is_null($experts)){
            if($this->load($data) and $this->validate()){
                if(isset($this->member_id)){
                    $experts->member_id = $this->member_id;
                }
                if(isset($this->name)){
                    $experts->name = $this->name;
                }
                if(isset($this->avatar)){
                    $experts->avatar = $this->avatar;
                }
                if(isset($this->authenticate_cost)){
                    $experts->authenticate_cost = $this->authenticate_cost;
                }
                if(isset($this->goodat)){
                    $experts->goodat = $this->goodat;
                }
                if(isset($this->identified)){
                    $experts->identified = $this->identified;
                }
                if(isset($this->introduce)){
                    $experts->introduce = $this->introduce;
                }
                if(isset($this->display)){
                    $experts->display = $this->display;
                }
                if($experts->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    public function getMember()
    {
        $info = $this->hasOne(Member::className(), ['member_id' => 'member_id'])
                    ->select(['{{%pj8_member}}.member_id', 'nickname', 'headimgurl']);
        return $info;
    }

    
}